Documentation for class string

Inherits from 	basic_object 

Properties declared in string 

Class Methods declared in string 

Methods declared in string 
	any 
		add(other_string) 
		add_first(new_char) 
		add_last(new_char) 
		as_capitalized 
		as_float 
		as_integer 
		as_lowercase 
		as_number 
		as_symbol 
		as_uppercase 
		at(index) 
		concatenate(other_string) 
		duplicate 
		file
		file_extension
		file_name
		first 
		includes(char) 
		index_of(char) 
		insert(new_char, index) 
		insert_string(string, index) 
		is_capitalized 
		is_empty 
		is_equal_to(object) 
		is_greater_than(string) 
		is_greater_than_or_equal(string) 
		is_less_than(string) 
		is_less_than_or_equal(string) 
		is_lowercase 
		is_uppercase 
		last 
		length 
		match(pattern, match_type) 
		occurrences_of(char) 
		path 
		read_from_file(path) 
		remove(char) 
		remove_all(char) 
		remove_at(index) 
		remove_duplicates 
		remove_first 
		remove_last 
		rename(name) 
		replace(old_char, new_char) 
		replace_all(old_char, new_char) 
		replace_at(index, new_char) 
		reverse 
		split(delimiter_char) 
		substring(index, length) 
		to_capitalized 
		to_lowercase 
		to_uppercase 
		word(index, delimiter_char) 
		write_to_file(path) 
	case 
		as_capitalized 
		as_lowercase 
		as_uppercase 
		is_capitalized 
		is_lowercase 
		is_uppercase 
		to_capitalized 
		to_lowercase 
		to_uppercase 
	predicates 
		is_capitalized 
		is_empty 
		is_equal_to(object) 
		is_lowercase 
		is_uppercase 
	remove 
		remove(char) 
		remove_all(char) 
		remove_at(index) 
		remove_duplicates 
		remove_first 
		remove_last 
	replace 
		replace(old_char, new_char) 
		replace_all(old_char, new_char) 
		replace_at(index, new_char) 

Class 
#begin_class
string 
The class string is a sub-class of the class basic_object. 
The syntax of a string is: 

"a string"

i.e. a double quote (") followed by a number of characters followed by another double quote (").

Use backslash (\) to protect quotes in a string.

Ex:
If you want to assign the variable s the string:
She said: "Hello!"
you type
s := "She said: \"Hello!\""
#end_class

Properties 

Methods 

#begin_method
add(other_string) 
Returns a new string with the string other_string added to the receiving string. 
You can also type "a string" + "abc".
Returns the new string.
Ex:
s := "a string"
s:add("abc")
returns "a stringabc"
s := "a string"
s + "abc"
returns "a stringabc"
Also see concatenate(other_string).
#end_method

#begin_method
add_first(new_char) 
Adds the character new_char at the beginning of the string.
Returns self.
Ex:
s := "a string"
s:add_first(\X)
returns "Xa string"
#end_method

#begin_method
add_last(new_char) 
Adds the character new_char at the end of the string.
Returns self.
Ex:
s := "a string"
s:add_last(\X)
returns "a stringX"
#end_method

#begin_method
as_capitalized 
Returns a copy of the string. The first character in the new string will be uppercase.
Ex:
s := "a string"
s:as_capitalized
returns "A string"
#end_method

#begin_method
as_float 
Returns the string as a float.
Ex:
s1 := "12"
s2 := "12.25"
s3 := "abc"
s1:as_float
returns 12.0
s2:as_float
returns 12.250000
s3:as_float
returns 0.000000
#end_method

#begin_method
as_integer 
Returns the string as an integer.
Ex:
s1 := "12"
s2 := "12.25"
s3 := "abc"
s1:as_integer
returns 12
s2:as_integer
returns 12
s3:as_integer
returns 0
#end_method

#begin_method
as_lowercase 
Returns a copy of the string. All the characters in the new string will be lowercase.
Ex:
s := "ABCabc123!@"
s:as_lowercase
returns "abcabc123!@"
#end_method

#begin_method
as_number 
Returns the string as a float or an integer.
Ex:
s1 := "12"
s2 := "12.25"
s3 := "abc"
s1:as_number
returns 12
s2:as_number
returns 12.250000
s3:as_number
returns 0.000000
#end_method

#begin_method
as_symbol 
Returns a symbol created with the characters in the string.
Ex:
s1 := "abc"
s2 := "13"
s3 := "a string"
s1:as_symbol
returns #abc
s2:as_symbol
returns #'13'
s3:as_symbol
returns #'a string'
#end_method

#begin_method
as_uppercase 
Returns a copy of the string. All the characters in the new string will be uppercase.
Ex:
s := "ABCabc123!@"
s:as_uppercase
returns "ABCABC123!@"
#end_method

#begin_method
at(index) 
Returns the character at the position index in the string. If index is higher than the number of elements in the collection or lower than 0 the method returns nil.
Note: The first character in a string has the index 1.
Ex:
s := "a string"
s:at(0)
returns nil
s:at(4)
returns \t
s:at(10)
returns nil
#end_method

#begin_method
concatenate(other_string) 
Adds the string other_string at the end of the receiving string.
Returns self.
Ex:
s := "a string"
s:concatenate("abc")
returns "a stringabc"
Note: The method concatenate changes the receiving string. The method add(other_string) have a similar behavior but returns a new string and may be safer to use.
Also see add(other_string).
#end_method

#begin_method
duplicate 
Returns a new string that is a copy of the receiving string.
#end_method

#begin_method
file 
Returns the filename if the string is a path. Otherwise the method returns nil.
Ex:
s := "/LocalLibrary/Foo/Bar/File.txt"
s:file
returns "File.txt"
#end_method

#begin_method
file_extension 
Returns the file extension if the string is a path or a filename. Otherwise the method returns nil.
Ex:
s := "/LocalLibrary/Foo/Bar/File.txt"
s:file_extension
returns "txt"
#end_method

#begin_method
file_name 
Returns the filename if the string is a path. Otherwise the method returns nil.
Ex:
s := "/LocalLibrary/Foo/Bar/File.txt"
s:file_name
returns "File"
#end_method

#begin_method
first 
Returns the first character of the string.
Ex:
s := "Hello"
s:first
returns \H
#end_method

#begin_method
includes(char) 
Returns true if the string contains at least one occurrence of char.
Ex:
s := "a string"
s:includes(\b)
returns false
s:includes(\r)
returns true
#end_method

#begin_method
index_of(char) 
Returns the index of the first occurrence of char. If there are no occurrence of char the method returns 0.
Ex:
s := "a string"
s:index_of(\s)
returns 3
s:index_of(\x)
returns 0
#end_method

#begin_method
insert(new_char, index) 
Inserts the character new_char at index in the string. If index is higher than the number of elements in the string the method returns nil.
Returns self.
Note: The first character in a string has the index 1.
Ex:
s := "a string"
s:insert(\X, 1)
returns "Xa string"
s:insert(\Y, 5)
returns "Xa sYtring"
s:insert(\Z, 13)
returns nil
#end_method

#begin_method
insert_string(string, index) 
Inserts the string string at index in the string. If index is higher than the number of elements in the string the method returns nil.
Returns self.
Note: The first character in a string has the index 1.
Ex:
s := "a string"
s:insert("ABC", 1)
returns "ABCa string"
s:insert("ABC", 5)
returns "ABCaABC string"
s:insert("ABC", 20)
returns nil
#end_method

#begin_method
is_capitalized 
Returns true if the first character of the string is uppercase. Otherwise the method returns false.
Ex:
s1 := "a string"
s2 := "A string"
s1:is_capitalized 
returns false
s1:is_capitalized 
returns true
#end_method

#begin_method
is_empty 
Returns true if the number of characters in the string is 0. Otherwise the method returns false.
Ex:
s1 := "a string"
s2 := ""
s1:is_empty
returns false
s2:is_empty
returns true
#end_method

#begin_method
is_equal_to(object) 
Returns true if all the characters of the string are equal to the corresponding characters in the string object. Otherwise the method returns false.
Ex:
a := "abc"
b := "abc"
c := "xyz"
a:is_equal_to(b)
returns true
a:is_equal_to(c)
returns false
Note: These are equivalent to a = b and a = c.
#end_method

#begin_method
is_greater_than(string) 
Returns true if the string is greater than string. Otherwise the method returns false.
Ex:
"abc":is_greater_than("bcd")
returns false
"bcd":is_greater_than("bcd")
returns false
"cde":is_greater_than("bcd")
returns true
Note: These are equivalent to "abc" > "bcd", "bcd" > "bcd" and "cde" > "bcd".
#end_method

#begin_method
is_greater_than_or_equal(string) 
Returns true if the string is greater than or equal to string. Otherwise the method returns false.
Ex:
"abc":is_greater_than_or_equal("bcd")
returns false
"bcd":is_greater_than_or_equal("bcd")
returns true
"cde":is_greater_than_or_equal("bcd")
returns true
Note: These are equivalent to "abc" >= "bcd", "bcd" >= "bcd" and "cde" >= "bcd".
#end_method

#begin_method
is_less_than(string) 
Returns true if the string is less than string. Otherwise the method returns false.
Ex:
"abc":is_less_than("bcd")
returns true
"bcd":is_less_than("bcd")
returns false
"cde":is_less_than("bcd")
returns false
Note: These are equivalent to "abc" < "bcd", "bcd" < "bcd" and "cde" < "bcd".
#end_method

#begin_method
is_less_than_or_equal(string) 
Returns true if the string is less than or equal to string. Otherwise the method returns false.
Ex:
"abc":is_less_than_or_equal("bcd")
returns true
"bcd":is_less_than_or_equal("bcd")
returns true
"cde":is_less_than_or_equal("bcd")
returns false
Note: These are equivalent to "abc" <= "bcd", "bcd" <= "bcd" and "cde" <= "bcd".
#end_method

#begin_method
is_lowercase 
Returns true if all the characters in the string are lowercase. Otherwise the method returns false.
Ex:
a := "Hello"
b := "hello"
a:is_lowercase
returns false
b:is_lowercase
returns true
#end_method

#begin_method
is_uppercase 
Returns true if all the characters in the string are uppercase. Otherwise the method returns false.
Ex:
a := "Hello"
b := "HELLO"
a:is_uppercase
returns false
b:is_uppercase
returns true
#end_method

#begin_method
last 
Returns the last character of the string. If the string is empty the method returns nil.
Ex:
a := "Hello"
a:last
returns \o
#end_method

#begin_method
length 
Returns the number of character in the string.
Ex:
a := "Hello"
a:length
returns 5
#end_method

#begin_method
match(pattern, match_type) 
Returns the index of the first occurrence of pattern in the string. The pattern is a string and may be a regular expression (with wildcards etc). If there is no match the method returns false.
The symbol match_type determines the type of match:
#exactly means that pattern should match the string exactly.
#prefix means that pattern should match the beginning of a word in the string.
#within means that pattern should match any part of the string.
#word means that pattern should match a word in the string.
Ex:
s := "This is a test string!"
s:match("This is a string!", #exactly)
returns false
s:match("This is a test string!", #exactly)
returns true
s:match("es", #prefix)
returns false
s:match("tes", #prefix)
returns 11
s:match("hs", #within)
returns false
s:match("hi", #within)
returns 2
s:match("tes", #word)
returns false
s:match("test", #word)
returns 11
s:match("t*t", #within)  (a regular expression)
returns 11
#end_method

#begin_method
occurrences_of(char) 
Returns the number of occurrences of the character char in the string.
Ex:
s := "a test string"
s:occurrences_of(\s)
returns 2
#end_method

#begin_method
path 
Returns the path without the filename if the string is a path. Otherwise the method returns nil.
Ex:
s := "/LocalLibrary/Foo/Bar/File.txt"
s:path
returns "/LocalLibrary/Foo/Bar"
#end_method

#begin_method
read_from_file(path) 
Puts the contents of the file path into the string. If path is not a valid filename the method returns nil
Returns self.
Ex:
s := ""
s:read_from_file("/etc/hosts")
returns the contents of the file /etc/hosts. (Might be a lot)
#end_method

#begin_method
remove(char) 
Removes the first occurrence of the character char in the string. If the string does not contain char the method returns nil.
Returns self.
Ex:
s := "a string"
s:remove(\r)
returns "a sting"
s:remove(\e)
returns nil
#end_method

#begin_method
remove_all(char) 
Removes all occurrences of the character char in the string. If the string does not contain char the method returns nil.
Returns self.
Ex:
s := "a test string"
s:remove_all(\t)
returns "a es sring"
#end_method

#begin_method
remove_at(index) 
Removes the character at index in the string. If index is higher than the number of characters in the string the method returns nil.
Returns the removed character.
Ex:
s := "a string"
s:remove_at(3)
returns \t
s:remove_at(8)
returns nil
#end_method

#begin_method
remove_duplicates 
Removes all duplicates of all characters in the string.
Returns self.
Ex:
s := "a test string"
s:remove_duplicates
returns "a tesring"
#end_method

#begin_method
remove_first 
Removes the first character in the string. If the string is empty the method returns nil. (Equivalent to remove_at(1).)
Returns the removed character.
Ex:
s := "a string"
s:remove_first
returns \a
#end_method

#begin_method
remove_last 
Removes the last character in the string. If the string is empty the method returns nil.
Returns the removed character.
Ex:
s := "a string"
s:remove_last
returns \g
#end_method

#begin_method
rename(name) 
Sets the name of the string to the symbol name.
Ex:
s := "a string"
s:rename(#my_string)
The string c can now also be referred to as
{string my_string}
and returns "a string"
#end_method

#begin_method
replace(old_char, new_char) 
Replaces the first occurrence of old_char with new_char.
Returns self.
Ex:
s := "a string"
s:replace(\i, \o)
returns "a strong"
#end_method

#begin_method
replace_all(old_char, new_char) 
Replaces all occurrences of old_char with new_char.
Returns self.
Ex:
s := "a test string"
s:replace_all(\t, \x)
returns "a xesx sxring"
#end_method

#begin_method
replace_at(index, new_char) 
Replaces the character at index with new_char. If index is higher than the number of characters in the string the method returns nil.
Returns the replaced character.
Ex:
s := "a string"
s:replace(4, \X)
returns \t
s:replace(10, \X)
returns nil
#end_method

#begin_method
reverse 
Reverses the order of the characters in the string.
Returns self.
Ex:
s := "a string"
s:reverse
returns "gnirts a"
#end_method

#begin_method
split(delimiter_char) 
Returns a collection of the sub-strings of the string delimited by the character delimiter_char.
Ex:
s := "an example of a string"
s:split(\space)
returns ["an" "example" "of" "a" "string"]
s:split(\a)
returns ["n ex" "mple of " " string"]
#end_method

#begin_method
substring(index, length) 
Returns a sub-string of the string starting from the character index. The new string will contain length characters. If index is out of bounds or length is less than 1 the method returns nil.
Ex:
s := "a string"
s:substring(3, 6)
returns "string"
s:substring(3, -6)
returns nil
#end_method

#begin_method
to_capitalized 
Makes the first character of the string uppercase.
Returns self.
Ex:
s := "a string"
s:to_capitalized
returns "A string"
#end_method

#begin_method
to_lowercase 
Makes all letters in the string lowercase.
Returns self.
Ex:
s := "ABCabc123!@"
s:to_lowercase
returns "abcabc123!@"
#end_method

#begin_method
to_uppercase 
Makes all letters in the string uppercase.
Returns self.
Ex:
s := "ABCabc123!@"
s:to_uppercase
returns "ABCABC123!@"
#end_method

#begin_method
word(index, delimiter_char) 
Returns a sub-string that is the word index of the string. A "word" is defined by the delimiter_char. Usually this is \space.
Ex:
s := "an example of a string"
s:word(3, \space)
returns "of"
s:word(3, \a)
returns " string"
#end_method

#begin_method
write_to_file(path) 
Writes the string to the file path. If path is not a complete filename the file will be placed in the home directory. If path is not a valid filename or if you do not have write permission the method returns nil.
Returns self.
Ex:
s := "an example of a string"
s:write_to_file("MyFile")
creates the file MyFile in the home directory with the string.
#end_method

Class Methods